package com.ywjt.platformbusiness.market.timelimitgoods.adapter;

import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.market.hagglegoods.model.GroupListModel;
import com.ywjt.platformbusiness.market.timelimitgoods.model.LimitTimeGoodsListModel;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class TimeLimitGoodsAdapter extends BaseQuickAdapter<LimitTimeGoodsListModel.ResultDTO.RecordsDTO, BaseViewHolder> {
    private Timer mTimer;
    TextView tvTimeDown;
    public TimeLimitGoodsAdapter(@Nullable List<LimitTimeGoodsListModel.ResultDTO.RecordsDTO> data) {
        super(R.layout.item_timelimitgoods, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, LimitTimeGoodsListModel.ResultDTO.RecordsDTO item) {
        mTimer=new Timer();
        ImageView ivGoodsImg=helper.getView(R.id.ivGoodsImg);
         tvTimeDown=helper.getView(R.id.tvTimeDown);
        ProgressBar progesss1=helper.getView(R.id.progesss1);
        Glide.with(mContext).load(item.getCommodityMessage().getMainPhoto())
                .into(ivGoodsImg);
        TextView tvOldPrice=helper.getView(R.id.tvOldPrice);
        tvOldPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
        helper.setText(R.id.tvOldPrice,"¥"+item.getCommodityMessage().getPrice());
        helper.setText(R.id.tvPrice,"¥"+item.getPrice());
        helper.setText(R.id.tvGoodsName,item.getCommodityMessage().getName());
        helper.setText(R.id.tvGroupedNum,"已抢"+(item.getSales()==null?0:item.getSales()));
        progesss1.setMax(item.getStock());
        progesss1.setProgress(item.getSales().intValue());

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        long  time=System.currentTimeMillis();
        try {
            Date date2=df.parse(item.getEndTime());
            compTime(time, date2.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        startRun();
    }

    /***
     * 活动倒计时相关
     * **/
    int  mDay;
    int  mHour;
    int  mMin;
    int  mSecond;

    public void compTime(long  d1,long d2){
        long diff = d2 - d1;
         mDay=(int)(diff/(24*60*60*1000));
          mHour=(int)((diff/(60*60*1000)-mDay*24));
          mMin=(int)(((diff/(60*1000))-mDay*24*60-mHour*60));
          mSecond=(int)((diff/1000-mDay*24*60*60-mHour*60*60-mMin*60));
    }


    //活动报名倒计时
    private void startRun() {
        TimerTask mTimerTask = new TimerTask() {
            @Override
            public void run() {
                Message message = Message.obtain();
                message.what = 1;
                timeHandler.sendMessage(message);
            }
        };
        mTimer.schedule(mTimerTask,0,1000);
    }

    private Handler timeHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                computeTime();
                 tvTimeDown.setText(
                         "距结束"+(mDay<1?"":mDay+"天")+getTv(mHour)+":"+getTv(mMin)+":"+getTv(mSecond));
                if (mSecond == 0 &&  mDay == 0 && mHour == 0 && mMin == 0 ) {
                    mTimer.cancel();
                }
            }
        }
    };

    private String getTv(long l){
        if(l>=10){
            return l+"";
        }else{
            return "0"+l;//小于10,,前面补位一个"0"
        }
    }

    /**
     * 倒计时计算
     */
    private void computeTime() {
        mSecond--;
        if (mSecond < 0) {
            mMin--;
            mSecond = 59;
            if (mMin < 0) {
                mMin = 59;
                mHour--;
                if (mHour < 0) {
                    // 倒计时结束
                    mHour = 23;
                    mDay--;
                    if(mDay < 0){
                        // 倒计时结束
                        mDay = 0;
                        mHour= 0;
                        mMin = 0;
                        mSecond = 0;
                    }
                }
            }
        }
    }
}
